//
// Created by Martin on 2022/4/8.
//

#ifndef LEETCODE_CPP_102_BINARY_TREE_LEVEL_ORDER_TRAVERSAL_HPP
#define LEETCODE_CPP_102_BINARY_TREE_LEVEL_ORDER_TRAVERSAL_HPP

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
#ifdef __GNUC__
#include <bits/stdc++.h>

#else
#include <vector>
#include <queue>

#endif // __GNUC__

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> res;
        if (!root) return res;

        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty()) {
            vector<int> tmp;
            int size = q.size();
            while (size--) {
                TreeNode* p = q.front(); q.pop();
                tmp.push_back(p->val);
                if (p->left) q.push(p->left);
                if (p->right) q.push(p->right);
            }
            res.push_back(tmp);
        }
        return res;
    }
};

#endif //LEETCODE_CPP_102_BINARY_TREE_LEVEL_ORDER_TRAVERSAL_HPP
